package cn.easy.mortgage.service;

import cn.easy.base.utils.ExtractUtil;
import cn.easy.mortgage.bean.HjPGSearchHouseBean;
import cn.easy.mortgage.bean.HjPGSearchHouseResultBean;
import cn.easy.mortgage.bean.HjPGSingleQueryBean;
import cn.easy.mortgage.bean.HjPGSingleQueryResultBean;
import cn.easy.mortgage.utils.AESUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springside.modules.utils.mapper.JsonMapper;

import java.util.Map;

@Service
public class HjPgService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${hjpg.webservice.url}")
    private String webServiceUrl = "http://60.191.111.117:17470/wsService/SystemInquiryZXLHService?wsdl";
    @Value("${hjpg.username}")
    private String username = "test";
    @Value("${hjpg.password}")
    private String password = "000000";

    private Client client ;

    private String invoke4Objects(String operationName, String params) {
        logger.info("~~~~~~~~~~~~webService接口 " + operationName + " 调用开始~~~~~~~~~~~~~~~，参数：" + StringUtils.join(params, ","));

        try {
            if (client == null) {
                JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
                client = dcf.createClient(webServiceUrl);
                //设置超时
                setTimeout(client);
            }

            Object[] objects = client.invoke(operationName, AESUtil.encrypt(params));

            String result = AESUtil.decrypt(objects[0].toString());

            logger.info("~~~~~~~~~~~~ " + operationName + " 调用成功:" + StringUtils.join(result, ","));

            return result;
        } catch (Exception e) {
            logger.error("~~~~~~~~~~~~ " + operationName + " 调用失败:", e.getMessage());
        }
        return null;
    }

    public HjPGSearchHouseResultBean searchHouse(HjPGSearchHouseBean hjPGSearchHouseBean) {
        String result = invoke4Objects("searchHouse", toParamsJson(hjPGSearchHouseBean));

        return JsonMapper.defaultMapper().fromJson(result, HjPGSearchHouseResultBean.class);
    }

    public HjPGSingleQueryResultBean singleQuery(HjPGSingleQueryBean hjPGSingleQueryBean) {
        String result = invoke4Objects("singleQuery", toParamsJson(hjPGSingleQueryBean));

        return JsonMapper.defaultMapper().fromJson(result, HjPGSingleQueryResultBean.class);
    }

    private String toParamsJson(Object obj) {
        Map<String, Object> map = ExtractUtil.object2Map(obj);
        map.put("userName", username);
        map.put("password", password);
        return JsonMapper.defaultMapper().toJson(map);
    }

    /**
     * 设置链接超时
     *
     * @param client
     */
    private void setTimeout(final Client client) {
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(5000);  //连接超时
        httpClientPolicy.setAllowChunking(false);    //取消块编码
        httpClientPolicy.setReceiveTimeout(8000);     //响应超时
        http.setClient(httpClientPolicy);
    }

}
